package acm.天梯赛练习2;


import java.util.*;

public class M {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        char[] chars = sc.nextLine().toCharArray();
        Tree norm = Tree.create(chars);
        List<Integer> preorder =new ArrayList<>(chars.length);
//        List<Integer> inorder=new ArrayList<>(chars.length);
        Tree.preorder(preorder,norm);
//        Tree.inorder(inorder,norm);
        while (n-->0){
            char[] chars1 = sc.nextLine().toCharArray();
            Tree tree = Tree.create(chars1);
            List<Integer> list1=new ArrayList<>(chars1.length);
//            List<Integer> list2=new ArrayList<>(chars1.length);
            Tree.preorder(list1,tree);
//            Tree.inorder(list2,tree);
//            System.out.println(preorder.equals(list1)&&inorder.equals(list2)?"YES":"NO");
            System.out.println(preorder.equals(list1)?"YES":"NO");
        }
    }

    static class Tree{
        int val;
        Tree left;
        Tree right;
        public Tree(int val) {
            this.val = val;
        }

        public void insert(int val){
            Tree parent=null;
            Tree curr = this;
            while (curr!=null){
                parent=curr;
                curr=curr.val>val?curr.left:curr.right;
            }
            if (parent.val>val) {
                parent.left=new Tree(val);
            }else {
                parent.right=new Tree(val);
            }
        }

        //先序遍历
        public static void preorder(List<Integer> list,Tree tree){
            if (tree==null){
                return;
            }
            list.add(tree.val);
            preorder(list,tree.left);
            preorder(list,tree.right);
        }

        public static void inorder (List<Integer> list,Tree tree){
            if (tree==null){
                return;
            }
            preorder(list,tree.left);
            list.add(tree.val);
            preorder(list,tree.right);
        }

        public static Tree create(char[] val){
            Tree root = new Tree(val[0]);
            for (int i = 1; i < val.length; i++) {
                root.insert(val[i]);
            }
            return root;
        }
    }

//        public boolean equals(Tree other) {
//            if (other==null){
//                return false;
//            }
//            Queue<Tree> queue=new LinkedList<>();
//            Tree self;
//            queue.add(this);
//            queue.add(other);
//            while (!queue.isEmpty()){
//                self = queue.poll();
//                other = queue.poll();
//
//                if (self==null&&other==null){   //两个都等于null
//                    continue;
//                }else if (self==null||other==null){ //有一个不等于null
//                    return false;
//                }
//                //两个都不等于null
//                if (self.val!=other.val){
//                    return false;
//                }
//                queue.add(self.left);
//                queue.add(other.left);
//                queue.add(self.right);
//                queue.add(other.right);
//            }
//            return true;
//        }
}
